----------------------------------------------------------------------------------
-- Company: 
-- Engineer: 
-- 
-- Create Date:    20:07:44 04/19/2011 
-- Design Name: 
-- Module Name:    Recorder - Behavioral 
-- Project Name: 
-- Target Devices: 
-- Tool versions: 
-- Description: 
--
-- Dependencies: 
--
-- Revision: 
-- Revision 0.01 - File Created
-- Additional Comments: 
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

entity Recorder is
    Port ( CLOCK : in  STD_LOGIC;
			  START_RECORDING : in STD_LOGIC;
           ADC_BUSY : in  STD_LOGIC;
           RS232_BUSY : in  STD_LOGIC;
           CHANNEL_A : in  STD_LOGIC_VECTOR (15 downto 0);
           CHANNEL_B : in  STD_LOGIC_VECTOR (15 downto 0);
			  GAIN_0 : in  STD_LOGIC;
			  GAIN_1 : in  STD_LOGIC;
			  GAIN_2 : in  STD_LOGIC;
			  GAIN_3 : in  STD_LOGIC;
			  LED_0 : out STD_LOGIC;
			  LED_1 : out STD_LOGIC;
			  LED_2 : out STD_LOGIC;
			  LED_3 : out STD_LOGIC;
			  LED_RS232_ERROR : out STD_LOGIC;
			  ADC_GAIN_INIT : out  STD_LOGIC;
			  ADC_RECORD_SAMPLE : out  STD_LOGIC;
			  GAIN_OUT : out STD_LOGIC_VECTOR (7 downto 0);
           RS232_DATA_SEND : out  STD_LOGIC;
           RS232_DATA : out  STD_LOGIC_VECTOR (7 downto 0));
end Recorder;

architecture Behavioral of Recorder is
	type MAIN_STATE_TYPE is (
		WAIT_START_RECORDING,
		INITIALIZE_GAIN,
		WAIT_FOR_GAIN,
		START_SAMPLING,
		WAIT_FOR_SAMPLE,
		SEND_CHANNEL_A_HIGH,
		SEND_CHANNEL_A_HIGH_START,
		WAIT_FOR_SEND_CHANNEL_A_HIGH,
		RS_232_FAILD
	); 
		
	signal MAIN_STATE : MAIN_STATE_TYPE := WAIT_START_RECORDING;
	signal MAIN_STATE_NEXT_STATE : MAIN_STATE_TYPE := WAIT_START_RECORDING;
	
	signal COUNTER_COUNT_ENABLE : STD_LOGIC := '0';
	signal COUNTER_RESET : STD_LOGIC := '0';
	signal COUNTER : STD_LOGIC_VECTOR (11 downto 0) := X"000";
begin
	process (CLOCK)
	begin
		if (rising_edge (CLOCK)) then
			MAIN_STATE <= MAIN_STATE_NEXT_STATE;
		end if;
	end process;
	
	process (MAIN_STATE,START_RECORDING,COUNTER,ADC_BUSY,RS232_BUSY)
	begin	
		case (MAIN_STATE) is
			when WAIT_START_RECORDING =>
				if (START_RECORDING = '1') then
					MAIN_STATE_NEXT_STATE <= INITIALIZE_GAIN;
				else
					MAIN_STATE_NEXT_STATE <= WAIT_START_RECORDING;
				end if;
			when INITIALIZE_GAIN =>
				MAIN_STATE_NEXT_STATE <= WAIT_FOR_GAIN;
			when WAIT_FOR_GAIN =>
				if (ADC_BUSY = '0') then
					MAIN_STATE_NEXT_STATE <= START_SAMPLING;
				else
					MAIN_STATE_NEXT_STATE <= WAIT_FOR_GAIN;
				end if;
			when START_SAMPLING =>
				MAIN_STATE_NEXT_STATE <= WAIT_FOR_SAMPLE;
			when WAIT_FOR_SAMPLE =>
				if (ADC_BUSY = '0') then
					MAIN_STATE_NEXT_STATE <= SEND_CHANNEL_A_HIGH;
				else
					MAIN_STATE_NEXT_STATE <= WAIT_FOR_SAMPLE;
				end if;
			when SEND_CHANNEL_A_HIGH =>
				MAIN_STATE_NEXT_STATE <= SEND_CHANNEL_A_HIGH_START;
			when SEND_CHANNEL_A_HIGH_START =>
				MAIN_STATE_NEXT_STATE <= WAIT_FOR_SEND_CHANNEL_A_HIGH;
			when WAIT_FOR_SEND_CHANNEL_A_HIGH =>
				if (COUNTER <= X"C35") then
					if (RS232_BUSY = '0') then
						MAIN_STATE_NEXT_STATE <= START_SAMPLING;
					else
						MAIN_STATE_NEXT_STATE <= WAIT_FOR_SEND_CHANNEL_A_HIGH;
					end if;
				else
					MAIN_STATE_NEXT_STATE <= RS_232_FAILD;
				end if;
			when RS_232_FAILD =>
				MAIN_STATE_NEXT_STATE <= RS_232_FAILD;
		end case;
	end process;
	
	process (MAIN_STATE,GAIN_0,GAIN_1,GAIN_2,GAIN_3,CHANNEL_A)
	begin		
		ADC_GAIN_INIT <= '0';
		ADC_RECORD_SAMPLE <= '0';
		RS232_DATA_SEND <= '0';
	   RS232_DATA <= X"00";
		GAIN_OUT <= GAIN_3 & GAIN_2 & GAIN_1 & GAIN_0 & GAIN_3 & GAIN_2 & GAIN_1 & GAIN_0;
		LED_0 <= GAIN_0;
		LED_1 <= GAIN_1;
		LED_2 <= GAIN_2;
		LED_3 <= GAIN_3;
		LED_RS232_ERROR <= '0';
		COUNTER_COUNT_ENABLE <= '1';
		COUNTER_RESET <= '0';
		
		case (MAIN_STATE) is
			when WAIT_START_RECORDING =>
				COUNTER_COUNT_ENABLE <= '0';
			when INITIALIZE_GAIN =>
				ADC_GAIN_INIT <= '1';
				COUNTER_COUNT_ENABLE <= '0';
			when WAIT_FOR_GAIN =>
				ADC_RECORD_SAMPLE <= '1';
				COUNTER_COUNT_ENABLE <= '0';
			when START_SAMPLING =>
				COUNTER_RESET <= '1';
			when WAIT_FOR_SAMPLE =>
			when SEND_CHANNEL_A_HIGH =>
				RS232_DATA <= CHANNEL_A (15 downto 8);
			when SEND_CHANNEL_A_HIGH_START =>
				RS232_DATA_SEND <= '1';
				RS232_DATA <= CHANNEL_A (15 downto 8);
			when WAIT_FOR_SEND_CHANNEL_A_HIGH =>
			when RS_232_FAILD =>
				LED_RS232_ERROR <= '1';
		end case;
	end process;

--######################################################################
--								Counters
--######################################################################	
	process (CLOCK,COUNTER_RESET)
	begin
		if (COUNTER_RESET = '1') then
			COUNTER <= X"000";
		else
			if (rising_edge (CLOCK)) then
				if (COUNTER_COUNT_ENABLE = '1') then
					COUNTER <= COUNTER + 1;
				end if;
			end if;
		end if;
	end process;
	
end Behavioral;

